import time
from socket import socket, AF_INET, SOCK_DGRAM


class BaseDataHex:
    sector = "010302000cf4010102907392000c"
    north = "010302000bd40101011fa4000c"


class BaseData:

    def __init__(self):
        self.arr_data = None
        self.send_data = bytearray()

    def get_data_type(self, base=16):
        """
        获取数据雷达数据类型
        :param base:
        :return:
        """
        data_type = int(self.arr_data[0:1], base)
        print(f"数据类型：{data_type}, 原始数据： {self.arr_data}")
        return data_type

    def set_data(self, data):
        self.arr_data = data
        self.restructuring_data()

    def restructuring_data(self):
        """
        重组原始报文
        @return:
        """
        for i in range(int(len(self.arr_data) / 2)):
            self.send_data.append(int(self.arr_data[2 * i:2 * (i + 1)], 16))


class UdpClient(BaseData):

    def __init__(self):
        super().__init__()
        self.address = None
        self.client_socket = socket(AF_INET, SOCK_DGRAM)

    def connect(self, host, port):
        """
        创建连接
        :param host:
        :param port:
        :return:
        """
        self.address = (host, port)

    def close(self):
        """
        关闭socket
        :return:
        """
        self.client_socket.close()

    def send(self):
        """
        发送数据
        """
        self.client_socket.sendto(self.send_data, self.address)
        self.send_data.clear()

    def __send(self, is_print_log: bool = False):
        """
        发送数据
        @param is_print_log: 打印发送的数据
        @return:
        """
        if is_print_log:
            print(self.send_data)
        self.client_socket.sendto(self.send_data, self.address)
        self.send_data.clear()

    def main_send(self, is_print_log: bool = False, is_send_sector: bool = True, is_send_north: bool = True):
        """
        发送数据
        @param is_print_log: 打印发送的数据
        @param is_send_sector: 发送扇区数据
        @param is_send_north: 发送正北数据
        @return:
        """
        self.__send(is_print_log)
        if is_send_sector:  # 发送32次扇区数据
            for i in range(32):
                self.set_data(BaseDataHex.sector)
                self.__send(is_print_log)
        if is_send_north:  # 发送一次正北数据
            self.set_data(BaseDataHex.north)
            self.__send(is_print_log)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    @staticmethod
    def sleep(t):
        time.sleep(t)


def send_data(host: str, port: str | int, dat_hex: str, is_print_log: bool = False, is_send_sector: bool = True,
              is_send_north: bool = True,
              is_get_data_type: bool = False):
    """
    发送数据
    @param port:  发送端口
    @param host:  发送地址
    @param dat_hex:  发送数据
    @param is_print_log:  是否打印日志
    @param is_send_sector:  是否发送扇区数据，默认发送
    @param is_send_north:  是否发送正北数据，默认发送
    @param is_get_data_type:  是否打印数据类型
    @return:
    """
    with UdpClient() as __client:
        __client.set_data(dat_hex)
        if is_get_data_type:
            __client.get_data_type()
        __client.connect(host, port)
        __client.main_send(is_print_log=is_print_log, is_send_sector=is_send_sector, is_send_north=is_send_north)


if __name__ == '__main__':
    from tqdm import trange

    HOST = "233.1.21.1"
    PORT = 56019
    LostHeight = "150038c71d3b49800c00290016661d4a9c3c0b330e8e254e1e0b000bb9065a1f0008076bf149ff5008060101060002e12038353530c30c40"

    for _ in trange(99999):
        send_data(host=HOST, port=PORT, dat_hex=LostHeight, is_send_sector=True, is_send_north=True)
        time.sleep(1)

    print('test')
